$.namespace('oa.facility.workOrderProcess');

oa.facility.workOrderProcess = function(){
	return {
		init : function(){
			EasyUIInit.initLayout({
				captionWidth : 100,
				fieldWidth : 200,
				fieldHeight : 30,
				rowHeight : 40,
				columns : 2,
				controls : this.config.fm
			}, 'fm');
			
			this.loadData();
			$('#fileUploadfm').ajaxForm({
				url: ctxRest + '/sys/file/upload',
				type: 'POST', 
				resetForm: true,
				beforeSend : function(){ progressLoad(); },
				success: function (data) {
					progressClose();
					$('#fileUploadDlg').dialog('close');
		            $('#imgs').append('<span url="' + data + '" ><img src="' + ctxRest + '/sys/file/get?path=' + data + '" style="width:100px;height:100px;" /><span style="position:relative;top:12px;left:-60px;"><a href="javascript:void(0);" onClick="$(this).parent().parent().remove()">删除</a></span></span>');
				}
			});
		},
		loadData : function(){
			// 流程数据
			$('#taskId').val(getParameter('act.taskId'));
			$('#procInsId').val(getParameter('act.procInsId'));
			// 业务数据回显
			$.ajax({
				type : 'GET',
				url : ctxRest + '/oa/facility/workorder/get?id=' + getParameter('id'),
				contentType : 'application/json;charset=UTF-8',
				beforeSend : function(){ progressLoad();},
				success : function(result){//console.log(result);
					progressClose();
					$('#id').val(result.id);
					EasyUIInit.loadData("fm", result);
					// 图片回显
					if(result.pic){
						var pics = result.pic.split(',');
						for (var i = 0;i < pics.length;i++) {
							if (pics[i]) {
								$('#pics').append('<a target="_blank" style="float:left;margin-right:5px;" href="' + ctxRest + '/sys/file/get?path=' + pics[i] + '"><img src="' + ctxRest + '/sys/file/get?path=' + pics[i] + '" style="width:100px;height:100px;"/></a>');
							}
						}
					}
				}
			});
		},
		showFileUploadForm : function(){
			$('#fileUploadDlg').dialog('open');
		},
		getFormData : function(){
			var obj;
			var fm = $('#fm');
			if (fm.form('validate')) {
				obj = $.serializeObject(fm);
				obj.paramMap = {
					commit : obj.commit
				};
				obj.act = {
					taskId : obj.taskId,
					procInsId : obj.procInsId
				};
				delete obj.commit;
				delete obj.taskId;
				delete obj.procInsId;
				
				var attachments = [];
				$('#imgs').find('span').each(function(){
					if ($(this).attr('url')) {
						var item = {
							url : $(this).attr('url'),
							attachmentDescription : '',
							attachmentName : '',
							attachmentType : 'image'
						};
						attachments.push(item);
					}
				});
				obj.paramMap.attachments = attachments;
			}
			return obj;
		},
		process : function(pass) {
			var obj = this.getFormData();
			obj.paramMap.pass = pass;//console.log(JSON.stringify(obj));return;
			var url = '';
			switch (pass) {
				case 0:	// 回退
					url	 = ctxRest + '/oa/facility/workorder/back';
					break;
				case 1:	// 提交审核
					url	 = ctxRest + '/oa/facility/workorder/toAudit';
					break;
				default:// 处理 (2)
					url	 = ctxRest + '/oa/facility/workorder/process';
					break;
			}
			if(obj && url) {
				$.ajax({
					type : 'POST',
					url	 : url,
					contentType : 'application/json;charset=utf-8',
					data : JSON.stringify(obj),
					beforeSend : function(){ progressLoad(); },
					success : function(result){
						//progressClose();
						$.messager.alert('提示','操作成功', 'info');
						window.location.href = ctx + '/page/act/taskTodoList';
					}
				})
			}
		}
	}
}();

oa.facility.workOrderProcess.config = {
	fm : [
			{id:'taskId',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
			{id:'procInsId',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
			{id:'id',type:'input',isExternal:true,attr:'hidden=true',colspan:0},
			{id:'gridName',caption:'广场名称：',type:'span',isExternal:true},
			{id:'regionName',caption:'广场区域：',type:'span',isExternal:true},
			{id:'facilityCode',caption:'设施编号：',type:'span',isExternal:true,colspan:1} ,
			{id:'facilityName',caption:'设施名称：',type:'span',isExternal:true,colspan:1} ,
			{id:'typeName',caption:'业务类型：',type:'span',isExternal:true},
			{id:'sourceName',caption:'案件来源：',type:'span',isExternal:true},
			{id:'createTime',caption:'工单时间：',type:'span',isExternal:true},
			{id:'limitHour',caption:'期限(小时)：',type:'span',isExternal:true},

		{id:'content',caption:'内容：',type:'span',isExternal:true,colspan:2},
		{id:'pics',caption:'工单图片：',type:'div',isExternal:true,colspan:2},
		{id:'commit',caption:'处理意见：',type:'easyui-textbox',option:{height:60,multiline:true},colspan:2},
		{id:'btn',caption:'上传图片：',type:'easyui-linkbutton',attr:'onClick="oa.facility.workOrderProcess.showFileUploadForm()"',option:{width:80,text:'上传图片'},colspan:2},
		{id:'imgs',caption:'处理图片：',type:'div',isExternal:true,colspan:2},
		{id:'div_button',caption:'',captionVisible:true,type:'span',colspan:2,isExternal:true,style:'text-align: center',
			controls:[
	          	{id:'lb_1',caption:'',captionVisible:false,type:'easyui-linkbutton',style:'padding:2px',attr:'onClick="oa.facility.workOrderProcess.process(0)"',option:{text:"回退",width:80}},
	          	{id:'lb_2',caption:'',captionVisible:false,type:'easyui-linkbutton',style:'padding:2px',attr:'onClick="oa.facility.workOrderProcess.process(1)"',option:{text:"提交审核",width:80}},
	          	{id:'lb_3',caption:'',captionVisible:false,type:'easyui-linkbutton',style:'padding:2px',attr:'onClick="oa.facility.workOrderProcess.process(2)"',option:{text:"处理",width:80}}
	        ]	
		}
	]
}

$(function(){
	oa.facility.workOrderProcess.init();
})